<?php

namespace App\Http\Controllers\Purchase;

use App\Http\Controllers\BaseController;
use App\Models\Purchase\PurchaseOrder;
use App\Models\Purchase\PurchaseOrderSub;
use App\Models\Purchase\PurchasePrePayOrder;
use App\Services\DingDing\PurchasePayOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 预付款单
 */
class PurchasePrePayOrderController extends BaseController
{
    /**
     * 预付款单列表
     */
    public function getorderlist(Request $request)
    {

        $cpid = $this->mangerinfo['cpid'];
        $manger_id = $this->mangerinfo['id'];
        if ($this->mangerinfo['utype'] == 4) {
            $manger_id = 0; //查询所有订单
        }
        $list = PurchasePrePayOrder::getList($request, $cpid, $manger_id);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
    /**
     * 获取预付单详情
     *
     */
    public function getinfo(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $manger_id = $this->mangerinfo['id'];
        $id = $request->id ?? 0;
        $info = PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['info' => $info->toArray()]]);
    }

    /**
     * 预付款单修改
     */
    public function dopostedit(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $manger_id = $this->mangerinfo['id'];
        $id = $request->id ?? 0;
        $info = PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        $info = $info->toArray();
        if ($info['audit_status'] != 1 || $info['audit_status'] != 4) {
            return response()->json(['code' => '001', 'msg' => '审核中以及审核成功的无法修改']);
        }

        $bankinfo = is_array($request->bankinfo) ? $request->bankinfo : (is_array(json_decode($request->bankinfo, true)) ? json_decode($request->bankinfo, true) : []);
        if (!(isset($bankinfo['bank']) && $bankinfo['bank'] != '') || !(isset($bankinfo['bank_number']) && $bankinfo['bank_number'] != '') || !(isset($bankinfo['bank_card_no']) && $bankinfo['bank_card_no'] != '') || !(isset($bankinfo['contacts']) && $bankinfo['contacts'] != '') || !(isset($bankinfo['phone_number']) && $bankinfo['phone_number'] != '')) {
            return response()->json(['code' => '001', 'msg' => '账户信息缺失']);
        }
        $data = [
            'bank' => $bankinfo['bank'],
            'bank_number' => $bankinfo['bank_number'],
            'bank_card_no' => $bankinfo['bank_card_no'],
            'contacts' => $bankinfo['contacts'],
            'phone_number' => $bankinfo['phone_number'],
        ];

        $data['product_channel'] = strval($request->product_channel);
        $data['delivery_cycle'] = strval($request->delivery_cycle);
        $data['breed_name'] = strval($request->breed_name);

        $data['pay_money'] = intval($request->pay_money);
        $data['remark'] = strval($request->remark);
        $data['is_screenshot'] = strval($request->is_screenshot);
        $data['urgent_remark'] = intval($request->urgent_remark);
        $data['update_at'] = time();

        if ($info['pa_type'] == 3 || $info['pa_type'] == 4) {
            $data['totalcount'] = intval($request->totalcount);
            $data['price'] = intval($request->price);
            $data['archive_material'] = strval($request->archive_material);
            $data['archive_spec'] = strval($request->archive_spec);
            $data['pre_pay_money'] = intval($request->pre_pay_money);
            $data['collect_money'] = intval($request->collect_money);
            $data['collect_bank'] = strval($request->collect_bank);
            $data['collect_info'] = strval($request->collect_info);
            $data['check_account_mobile'] = strval($request->check_account_mobile);
        }

        PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->update($data);
        //修改表单数据--修改哪些字段


        return response()->json(['code' => '000', 'msg' => '修改成功']);
    }

    /**
     * 预付款单删除
     */
    public function doorderdel(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $manger_id = $this->mangerinfo['id'];
        $id = $request->id ?? 0;
        $info = PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        $info = $info->toArray();
        if ($info['audit_status'] != 1 && $info['audit_status'] != 4) {
            return response()->json(['code' => '001', 'msg' => '审核中以及审核成功的无法删除']);
        }


        // $sub_order = json_decode($info['sub_order'], true);//预付款单是整单生成
        DB::beginTransaction();
        try {

            PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->update(['is_del' => 2, 'del_at' => time()]);
            //恢复采购订单状态，可重新付款
            PurchaseOrderSub::where(['cpid' => $cpid, 'order_id' => $info['order_id']])
                // ->whereIn('id', $sub_order)//预付款单是整单生成
                ->update(['pay_status' => 1, 'pay_at' => 0]);

            PurchaseOrder::where(['id' => $info['order_id'], 'manger_id' => $manger_id, 'cpid' => $cpid])->update(['pay_status' => 1, 'pay_at' => 0]);
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return response()->json(['code' => '001', 'msg' => '采购订单删除失败' . $e->getMessage()]);
        }




        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 提交审批
     */
    public function doorderauditbydingding(Request $request)
    {
        $cpid = $this->mangerinfo['cpid'];
        $manger_id = $this->mangerinfo['id'];
        $id = $request->id ?? 0;
        $info = PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '无效参数']);
        }
        PurchasePayOrder::processCreate(); //发起审批
        PurchasePrePayOrder::where(['cpid' => $cpid, 'manger_id' => $manger_id, 'is_del' => 1, 'id' => $id])->update(['audit_status' => 2]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 审批通过结果如何通知
     * 审核通过后增加供应商余额
     */
}
